import { defineStore } from 'pinia'

// mock 数据
const mockCases = [
  {
    id: '1',
    type: 'civil',
    description: '因合同履行发生争议，申请法律援助。',
    materials: [
      { name: '身份证复印件', url: '', status: 'pending' }
    ],
    status: 'submitted'
  },
  {
    id: '2',
    type: 'criminal',
    description: '交通事故责任认定有争议。',
    materials: [
      { name: '报案证明', url: '', status: 'pending' }
    ],
    status: 'processing'
  }
]

export const useCaseStore = defineStore('caseStore', {
  state: () => ({
    currentCase: {
      id: '',
      type: '',
      description: '',
      materials: [],
      status: 'draft'
    },
    caseList: {
      items: [...mockCases],
      page: 1,
      pageSize: 10,
      total: mockCases.length
    },
    caseTypes: [
      { value: 'civil', label: '民事案件' },
      { value: 'criminal', label: '刑事案件' },
      { value: 'administrative', label: '行政案件' }
    ]
  }),
  actions: {
    initNewCase(caseType) {
      this.currentCase = {
        id: Date.now().toString(),
        type: caseType,
        description: '',
        materials: [],
        status: 'draft'
      }
    },
    saveCaseDescription(content) {
      this.currentCase.description = content
    },
    async uploadMaterial(file, name) {
      await new Promise(resolve => setTimeout(resolve, 500))
      const url = typeof file === 'string' ? file : URL.createObjectURL(file)
      this.currentCase.materials.push({
        name,
        url,
        status: 'pending'
      })
    },
    async submitCase() {
      if (!this.currentCase.description || this.currentCase.materials.length === 0) {
        throw new Error('案件描述和材料不能为空')
      }
      this.currentCase.status = 'submitted'
      this.caseList.items.unshift({ ...this.currentCase })
      this.caseList.total += 1
    },
    async loadUserCases(page = 1) {
      const start = (page - 1) * this.caseList.pageSize
      const end = start + this.caseList.pageSize
      this.caseList.page = page
      this.caseList.items = (this.caseList.items || []).slice(start, end)
    },
    async updateCaseStatus(status) {
      this.currentCase.status = status
      const index = this.caseList.items.findIndex(c => c.id === this.currentCase.id)
      if (index !== -1) {
        this.caseList.items[index] = { ...this.caseList.items[index], status }
      }
    }
  },
  getters: {
    requiredMaterials(state) {
      const materialMap = {
        civil: ['身份证复印件', '起诉状', '证据清单'],
        criminal: ['报案证明', '伤情鉴定', '证人名单'],
        administrative: ['行政处罚决定书', '行政复议材料']
      }
      return materialMap[state.currentCase.type] || []
    },
    isSubmittable(state) {
      return state.currentCase.description.trim() !== '' && state.currentCase.materials.length > 0
    },
    caseStatusText(state) {
      const statusMap = {
        draft: '草稿',
        submitted: '已提交',
        assigned: '已分配',
        processing: '处理中',
        closed: '已结案'
      }
      return statusMap[state.currentCase.status] || state.currentCase.status
    }
  }
}) 